<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
              <el-input v-model.number="listQuery.fwarringno" clearable class="filter-item" style="width: 200px;" placeholder="请输入报警编号 "/>
      <el-input v-model="listQuery.fsiteNo" clearable class="filter-item" style="width: 200px;" placeholder="请输入市/区/学校"/>
      <el-input v-model="listQuery.fpointNo" clearable class="filter-item" style="width: 200px;" placeholder="请输入教室编号"/>
      <el-input v-model="listQuery.fparafield" clearable class="filter-item" style="width: 200px;" placeholder="请输入参数字段"/>
      <el-date-picker v-model="listQuery.fgetdate" type="datetime" placeholder="请输入产生时间"></el-date-picker>
      <el-date-picker v-model="listQuery.fenddate" type="datetime" placeholder="请输入结束时间"></el-date-picker>
        <el-input v-model.number="listQuery.fprocflag" clearable class="filter-item" style="width: 200px;" placeholder="请输入"/>
      <el-input v-model="listQuery.fequipmentbarcode" clearable class="filter-item" style="width: 200px;" placeholder="请输入固定资产码"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button><!-- v-permission="[':tcWarring:read']"-->
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button><!-- v-permission="[':tcWarring:create']"-->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" stripe element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column align="right" label="报警编号 " prop="fwarringno" />
      <el-table-column align="right" label="市/区/学校" prop="fsiteNo" />
      <el-table-column align="right" label="教室编号" prop="fpointNo" />
      <el-table-column align="right" label="参数字段" prop="fparafield" />
      <el-table-column align="right" label="当前值" prop="fcurnum" />
      <el-table-column align="right" label="起始值" prop="fbegnum" />
      <el-table-column align="right" label="起讫值" prop="fendnum" />
      <el-table-column align="right" label="报警说明" prop="fdescribe" />
      <el-table-column :formatter="datetimeFormat"align="right" label="产生时间" prop="fgetdate" />
      <el-table-column :formatter="datetimeFormat"align="right" label="结束时间" prop="fenddate" />
      <el-table-column align="right" label="" prop="fprocflag" />
      <el-table-column align="right" label="固定资产码" prop="fequipmentbarcode" />
      <el-table-column align="center" label="操作员" prop="foperator" />
      <el-table-column align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" class="button-margin" @click="handleUpdate(scope.row)">编辑</el-button><!-- v-permission="[':tcWarring:update']"-->
          <el-button type="danger" size="mini" class="button-margin" @click="handleDelete(scope.row)">删除</el-button><!-- v-permission="[':tcWarring:delete']"-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="报警编号 " prop="fwarringno">
          <el-input v-model.number="dataForm.fwarringno"/>
        </el-form-item>
        <el-form-item label="市/区/学校" prop="fsiteNo">
          <el-input v-model="dataForm.fsiteNo"/>
        </el-form-item>
        <el-form-item label="教室编号" prop="fpointNo">
          <el-input v-model="dataForm.fpointNo"/>
        </el-form-item>
        <el-form-item label="参数字段" prop="fparafield">
          <el-input v-model="dataForm.fparafield"/>
        </el-form-item>
        <el-form-item label="当前值" prop="fcurnum">
          <el-input v-model="dataForm.fcurnum"/>
        </el-form-item>
        <el-form-item label="起始值" prop="fbegnum">
          <el-input v-model="dataForm.fbegnum"/>
        </el-form-item>
        <el-form-item label="起讫值" prop="fendnum">
          <el-input v-model="dataForm.fendnum"/>
        </el-form-item>
        <el-form-item label="报警说明" prop="fdescribe">
          <el-input v-model="dataForm.fdescribe"/>
        </el-form-item>
        <el-form-item label="产生时间" prop="fgetdate">
          <el-date-picker v-model="dataForm.fgetdate" type="datetime"/>
        </el-form-item>
        <el-form-item label="结束时间" prop="fenddate">
          <el-date-picker v-model="dataForm.fenddate" type="datetime"/>
        </el-form-item>
        <el-form-item label="" prop="fprocflag">
          <el-input v-model.number="dataForm.fprocflag"/>
        </el-form-item>
        <el-form-item label="固定资产码" prop="fequipmentbarcode">
          <el-input v-model="dataForm.fequipmentbarcode"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .button-margin {
    margin : 2px 0px;
  }
  .el-button+.el-button {
    margin-left: 1px;
  }
</style>

<script>
import { listtcWarring, createtcWarring, updatetcWarring, deletetcWarring } from '@/api/tcWarring'
import Pagination from '@/components/Pagination'
import moment from 'moment'
export default {
  name: 'ATcWarring',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fwarringno: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fparafield: undefined,
        fcurnum: undefined,
        fbegnum: undefined,
        fendnum: undefined,
        fdescribe: undefined,
        fgetdate: undefined,
        fenddate: undefined,
        fprocflag: undefined,
        fequipmentbarcode: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        fwarringno: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fparafield: undefined,
        fcurnum: undefined,
        fbegnum: undefined,
        fendnum: undefined,
        fdescribe: undefined,
        fgetdate: undefined,
        fenddate: undefined,
        fprocflag: undefined,
        fequipmentbarcode: undefined,
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fwarringno: [
          { required: true, message: '报警编号 不能为空' },
          { type: 'number', message: '报警编号 必须为数字值' },

        ],
        fsiteNo: [



        ],
        fpointNo: [
          { required: true, message: '教室编号不能为空' },


        ],
        fparafield: [
          { required: true, message: '参数字段不能为空' },


        ],
        fcurnum: [
          { required: true, message: '当前值不能为空' },


        ],
        fbegnum: [
          { required: true, message: '起始值不能为空' },


        ],
        fendnum: [
          { required: true, message: '起讫值不能为空' },


        ],
        fdescribe: [
          { required: true, message: '报警说明不能为空' },


        ],
        fgetdate: [
          { required: true, message: '产生时间不能为空' },


        ],
        fenddate: [



        ],
        fprocflag: [

          { type: 'number', message: '必须为数字值' },

        ],
        fequipmentbarcode: [



        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listtcWarring(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        fwarringno: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fparafield: undefined,
        fcurnum: undefined,
        fbegnum: undefined,
        fendnum: undefined,
        fdescribe: undefined,
        fgetdate: undefined,
        fenddate: undefined,
        fprocflag: undefined,
        fequipmentbarcode: undefined,
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createtcWarring(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加ATcWarring成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatetcWarring(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新ATcWarring成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除ATcWarring', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletetcWarring(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除ATcWarring成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
